<template>
  <div class="app-container">
    <el-form ref="form" style="width:25%" :model="hechengList" label-width="80px">

      <el-form-item label="道具碎片1">
        <el-select v-model="compound.compoundA" style="width: 100%" placeholder="请选择">
            <el-option
              v-for="item in propsList"
              :key="item.id"
              :label="item.propsName"
              :value="item.id"
            />
          </el-select>
      </el-form-item>

      <el-form-item label="道具碎片2">
        <el-select v-model="compound.compoundB" style="width: 100%" placeholder="请选择">
            <el-option
              v-for="item in propsList"
              :key="item.id"
              :label="item.propsName"
              :value="item.id"
            />
          </el-select>
      </el-form-item>

      <el-form-item label="道具碎片3">
        <el-select v-model="compound.compoundC" style="width: 100%" placeholder="请选择">
            <el-option
              v-for="item in propsList"
              :key="item.id"
              :label="item.propsName"
              :value="item.id"
            />
          </el-select>
      </el-form-item>

      <el-form-item label="合成道具">
        <el-select v-model="compound.compoundAll" style="width: 100%" placeholder="请选择">
            <el-option
              v-for="item in propsList"
              :key="item.id"
              :label="item.propsName"
              :value="item.id"
            />
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onsubmit()" style="margin-right:10px">提交</el-button>
        <a :href="'#/hecheng/index'">
        <el-button>取消</el-button>
        </a>
      </el-form-item>
    </el-form>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: auto;
  display: block;
}
</style>
<script>
import hechengpropApi  from '@/api/hecheng'
import axios from 'axios'
export default {

  data() {
    return {
      compound: {},
      hechengList:[],
      propsList:[]
    }
  },
  created() {
      let id = this.$route.query.id
      if(id){
        this.getHeChengPropsById(id)
      }
      this.getHeChengProps()
  },
  methods: {
    //根据id获取道具信息
    getHeChengPropsById(id){
      hechengpropApi.getHeChengPropsById(id).then(res => {
        console.log(res.data.props);
        this.compound = res.data.props
      }).then(hechengList =>{
          for(hecheng in hechengList){
            compound[i].name = hecheng.propsName
            console.log("fuzhi"+hecheng)
        }
      })
    },
    //点击提交
    onsubmit(){
      if(this.$route.query.id){
        //修改
        this.updateHeChengProps()
      }else{
          let count = 0
          var int = 0
          if(this.compound.compoundA == null){
              int = count+1;
          }
          if(this.compound.compoundB == null){
              int = count+1;
          }
          if(this.compound.compoundC == null){
              int = count+1;
          }
          if(this.compound.compoundAll == null){
              int = count+1;
          }
          if(int === 0){
                //添加
                this.addHeChengProps()
            }else{
              //弹窗提示
            this.$message({
                message: '碎片和合成道具都不能为空',
                type:"error"
            })
        }
      }
    },
    //更新道具
    updateHeChengProps(){
      hechengpropApi.updateHeChengProps(this.compound).then(res => {
          //弹窗提示
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          //跳转
          this.$router.push("/hecheng/index")
      })
    },
    //添加道具
    addHeChengProps(){
      hechengpropApi.addHeChengProps(this.compound).then(res => {
          
          
            //弹窗提示
            this.$message({
                message: '添加成功',
                type: 'success'
            });
            //跳转
            this.$router.push("/hecheng/index")
          
          
      })
    },
    //获取所有合成道具信息
    getHeChengProps(){
      hechengpropApi.getHeChengProps().then(res => {
        console.log("合成："+res.data.list);
        this.propsList = res.data.list
        // this.imageUrl = this.prop.propsAvatar;
      })
    },
}
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>